import store from "@/store";

const PermissionPlugin = {};
PermissionPlugin.install = function (Vue) {
  Vue.directive("hasAuth", {
    inserted(el, binding) {
      // 页面指令传递的参数
      const { value } = binding;
      // vuex存储的用户权限
      const permissions = store.getters && store.getters.getPermissions;

      if (value && value instanceof Array) {
        if (value.length > 0) {
          const hasPermission = permissions.some((permission) => {
            return value.includes(permission);
          });

          if (!hasPermission) {
            // 没有权限页面删除对应元素
            el.parentNode && el.parentNode.removeChild(el);
          }
        }
      } else {
        throw new Error("权限赋值错误");
      }
    },
  });
};

export default PermissionPlugin;
